//
//  PageView.swift
//  PhotoDemo
//
//  Created by qings on 16/1/24.
//  Copyright © 2016年 pukaila. All rights reserved.
//

import Foundation
import UIKit

class PageView : UIView {
    
    var nextButton:UIButton!
    var prevButton:UIButton!
    var nextButtonWidth:Int!
    var prevButtonWidth:Int!
    var prevBtnCallback:((page:Int)->(Void))?
    var nextBtnCallback:((page:Int)->(Void))?
    var pageNumCallback:((page:Int)->(Void))?
    var currentPageNum:Int!
    var oldPageNum:Int!
    var pageScrollView:PageScrollView!
    var pageScrollViewPageSize:Int!
    var pageScrollViewPageNumHeight:Int!
    var pageScrollViewPageNumWidth:Int!
    var pageScrollViewPageNumSpacing:Int!
    
    var callback:((page:Int)->(Void))?
    override init(frame: CGRect) {
        super.init(frame:frame)
    }
    
    func doInit(){
        initView()
        initEven()
    }
    
    func initView() {
        oldPageNum = currentPageNum
        let x = nextButtonWidth
        let y = 0
        let w = Int(frame.width) - nextButtonWidth - nextButtonWidth
        let h = Int(frame.height)
        pageScrollView = PageScrollView(frame: CGRectMake(CGFloat(x), CGFloat(y), CGFloat(w), CGFloat(h)))
        pageScrollView.pageSize = pageScrollViewPageSize
        pageScrollView.pageHeight = pageScrollViewPageNumHeight
        pageScrollView.pageWidth = pageScrollViewPageNumWidth
        pageScrollView.spacing = pageScrollViewPageNumSpacing
        pageScrollView.backgroundColor = UIColor.grayColor()
        pageScrollView.showsHorizontalScrollIndicator = true
        pageScrollView.currentPageNum = currentPageNum
        pageScrollView.addPageNumItem()
        pageScrollView.callback={(page)->Void in
            self.oldPageNum = self.currentPageNum
            self.currentPageNum = page
            self.pageScrollView.setColor(self.oldPageNum, color: self.pageScrollView.normalColor)
            self.pageScrollView.setColor(self.currentPageNum, color: self.pageScrollView.currentColor)
            self.callback!(page:self.currentPageNum)
            self.moveToCenterByPageNum(self.currentPageNum)
        }
        self.addSubview(pageScrollView)
        
        prevButton = initButton(CGRectMake(CGFloat(0), CGFloat(0), CGFloat(nextButtonWidth), CGFloat(h)))
        prevButton.setTitle("<", forState: UIControlState.Normal)
        prevButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        self.addSubview(prevButton)
        
        nextButton = initButton(CGRectMake(CGFloat(nextButtonWidth+w), CGFloat(0), CGFloat(nextButtonWidth), CGFloat(h)))
        nextButton.setTitle(">", forState: UIControlState.Normal)
        nextButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        self.addSubview(nextButton)
        
        self.backgroundColor = UIColor.whiteColor()
    }
    
    func initEven() {
        prevButton.addTarget(self, action: "prevBtnClick:", forControlEvents: UIControlEvents.TouchUpInside)
        nextButton.addTarget(self, action: "nextBtnClick:", forControlEvents: UIControlEvents.TouchUpInside)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initButton(frame: CGRect)->UIButton {
        let button:UIButton = UIButton(frame: frame)
        button.backgroundColor = UIColor.redColor()
        return button
    }
    
    func prevBtnClick(sender:AnyObject) {
        let btn: UIButton = sender as! UIButton
        if (currentPageNum > 1) {
            oldPageNum = currentPageNum
            currentPageNum = currentPageNum-1;
            pageScrollView.setColor(oldPageNum, color: pageScrollView.normalColor)
            pageScrollView.setColor(currentPageNum, color: pageScrollView.currentColor)
            prevBtnCallback!(page: currentPageNum)
            self.moveToCenterByPageNum(currentPageNum)
        }
    }
    func nextBtnClick(sender:AnyObject) {
        let btn: UIButton = sender as! UIButton
        if (currentPageNum < pageScrollViewPageSize) {
            oldPageNum = currentPageNum
            currentPageNum = currentPageNum+1;
            pageScrollView.setColor(oldPageNum, color: pageScrollView.normalColor)
            pageScrollView.setColor(currentPageNum, color: pageScrollView.currentColor)
            nextBtnCallback!(page: currentPageNum)
            self.moveToCenterByPageNum(currentPageNum)
        }
    }
    
    func moveToCenterByPageNum(currentPageNum:Int)->Void {
        let halfWidth = CGFloat(pageScrollView.frame.width/2)
        let tempWidth = CGFloat(currentPageNum * (pageScrollViewPageNumWidth + pageScrollViewPageNumSpacing) - (pageScrollViewPageNumWidth + pageScrollViewPageNumSpacing)/2)
        var newX = tempWidth - halfWidth
        if (newX > 0 && pageScrollView.contentSize.width > self.frame.width){
            if ((pageScrollView.contentSize.width - tempWidth) >= halfWidth) {
                pageScrollView.setContentOffset(CGPointMake(newX, 0), animated: true)
            } else {
                newX = pageScrollView.contentSize.width-pageScrollView.frame.width
                pageScrollView.setContentOffset(CGPointMake(newX, 0), animated: true)
            }
        } else {
            pageScrollView.setContentOffset(CGPointMake(0, 0), animated: true)
        }
    }
}

class PageScrollView : UIScrollView {
    var screenObject:CGRect=UIScreen.mainScreen().bounds;
    var pageSize : Int!
    var pageHeight: Int!
    var pageWidth: Int!
    var spacing: Int!
    var currentPageNum: Int!
    var callback:((page:Int)->(Void))!
    var currentColor:UIColor = UIColor.orangeColor()
    var normalColor:UIColor = UIColor.greenColor()
    
    func getPageNum(i:Int)->UIView {
        let pageNumView : UIView = UIView()
        let x:CGFloat = CGFloat((i-1) * pageWidth) + CGFloat((i-1)*spacing)
        let y:CGFloat = 0
        let width:CGFloat = CGFloat(pageWidth)
        let height:CGFloat = CGFloat(pageHeight)
        pageNumView.frame = CGRectMake(x, y, width, height)
        pageNumView.backgroundColor = normalColor
        
        let label:UILabel = UILabel(frame: CGRectMake(0, 0, width, height))
        label.text = String(i)
        label.textAlignment = NSTextAlignment.Center
        let tap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "pageNumClick:")
        pageNumView.addGestureRecognizer(tap)
        pageNumView.tag = i
        pageNumView.addSubview(label)
        return pageNumView
    }
    
    func pageNumClick(sender:AnyObject){
        let tap:UITapGestureRecognizer = sender as! UITapGestureRecognizer
        callback(page: sender.view!!.tag)
    }
    
    func setColor(page:Int, color:UIColor)->Void {
        self.subviews[page-1].backgroundColor = color
    }
    
    override init(frame: CGRect) {
        super.init(frame:frame)
    }
    
    func addPageNumItem() {
        let width = (self.pageWidth+spacing)*self.pageSize - spacing
        let height = self.pageHeight
        self.contentSize=CGSizeMake(CGFloat(width), CGFloat(height))
        for index in 1...self.pageSize {
            self.addSubview(getPageNum(index))
        }
        self.setColor(self.currentPageNum, color: currentColor)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}